package com.edu.lighthouse.service;

import com.edu.lighthouse.helper.OrgCodeGenerator;
import com.edu.lighthouse.helper.security.Encryption;
import com.edu.lighthouse.mapper.CardInfoMapper;
import com.edu.lighthouse.pojo.CardInfo;
import com.edu.lighthouse.pojo.CardMain;
import com.edu.lighthouse.pojo.vo.*;
import com.edu.lighthouse.struct.Copy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wufengfu
 */
@Service
public class CardService {

    CardInfoMapper cardInfoMapper;

    @Autowired
    public CardService(CardInfoMapper cardInfoMapper) {
        this.cardInfoMapper = cardInfoMapper;
    }

    public List<CardInfo> listCard(CardBaseVo cardBaseVo) {

        return cardInfoMapper.cardList(cardBaseVo);
    }

    public CardMain addCard(CardInfoVo cardInfoVo){

        CardMain cardMain = Copy.struct(cardInfoVo,CardMain.class);
        //other operate
        String cardCode = OrgCodeGenerator.generateMemberCard();
        String cardPass = "123456";
        cardMain.setCardCode(cardCode);
        cardMain.setCardPassword(Encryption.hash(cardPass));
        int row = cardInfoMapper.insertCard(cardMain);
        if (row <= 0) {
            return null;
        }
        cardMain.setCardPassword(cardPass);
        return cardMain;
    }

    public int addCardBatch(CardInfoVo cardInfoVo){

        List<CardMain> cardMainList = new ArrayList<>();
        for(int i = 0; i < cardInfoVo.getCount();i++){
            CardMain cardMain = Copy.struct(cardInfoVo,CardMain.class);
            //other operate
            String cardCode = OrgCodeGenerator.generateMemberCard();
            String cardPass = "123456";
            cardMain.setCardCode(cardCode);
//            cardMain.setCardPassword(Encryption.hash(cardPass));
            cardMain.setCardPassword(cardPass);
            cardMainList.add(cardMain);
        }
        return cardInfoMapper.insertCardBatch(cardMainList);
    }

}
